<template>
  <a-modal
    title="编辑房间预定"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="预定房间id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择预定房间id" v-decorator="['roomId', {rules: [{ required: true, message: '请选择预定房间id！' }]}]">
            <a-select-option v-for="(item,index) in roomIdData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="房间编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入房间编号" v-decorator="['roomCode', {rules: [{required: true, message: '请输入房间编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="预定人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入预定人" v-decorator="['reservation', {rules: [{required: true, message: '请输入预定人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="联系方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入联系方式" v-decorator="['phone', {rules: [{required: true, message: '请输入联系方式！'}]}]" />
        </a-form-item>
        <a-form-item
          label="最晚入住日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择最晚入住日期" v-decorator="['latestDate',{rules: [{ required: true, message: '请选择最晚入住日期！' }]}]" @change="onChangelatestDate"/>
        </a-form-item>
        <a-form-item
          label=" 定金（0.00）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="收款日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择收款日期" v-decorator="['collectionDate',{rules: [{ required: true, message: '请选择收款日期！' }]}]" @change="onChangecollectionDate"/>
        </a-form-item>
        <a-form-item
          label="收款方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择收款方式" v-decorator="['collectionType', {rules: [{ required: true, message: '请选择收款方式！' }]}]">
            <a-select-option v-for="(item,index) in collectionTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { roomReservationEdit } from '@/api/modular/main/roomreservation/roomReservationManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        roomIdData: [],
        latestDateDateString: '',
        collectionDateDateString: '',
        collectionTypeData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        const roomIdOption = this.$options
        this.roomIdData = roomIdOption.filters['dictData']('common_status')
        const collectionTypeOption = this.$options
        this.collectionTypeData = collectionTypeOption.filters['dictData']('cost_type')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              roomId: record.roomId,
              roomCode: record.roomCode,
              reservation: record.reservation,
              phone: record.phone,
              deposit: record.deposit,
              collectionType: record.collectionType
            }
          )
        }, 100)
        // 时间单独处理
        if (record.latestDate != null) {
            this.form.getFieldDecorator('latestDate', { initialValue: moment(record.latestDate, 'YYYY-MM-DD') })
        }
        this.latestDateDateString = moment(record.latestDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.collectionDate != null) {
            this.form.getFieldDecorator('collectionDate', { initialValue: moment(record.collectionDate, 'YYYY-MM-DD') })
        }
        this.collectionDateDateString = moment(record.collectionDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.latestDate = this.latestDateDateString
            values.collectionDate = this.collectionDateDateString
            roomReservationEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangelatestDate(date, dateString) {
        this.latestDateDateString = dateString
      },
      onChangecollectionDate(date, dateString) {
        this.collectionDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
